package com.hwadee.teachingprocessmanagementsystem.service.base.impl;

import com.hwadee.teachingprocessmanagementsystem.dto.MajorDto;
import com.hwadee.teachingprocessmanagementsystem.entity.Major;
import com.hwadee.teachingprocessmanagementsystem.dao.MajorDao;
import com.hwadee.teachingprocessmanagementsystem.service.base.MajorService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import jakarta.annotation.Resource;

import java.util.List;

/**
 * (Major)表服务实现类
 *
 * @author makejava
 * @since 2024-07-11 14:20:03
 */
@Service("majorService")
public class MajorServiceImpl implements MajorService {
    @Resource
    private MajorDao majorDao;

    /**
     * 通过ID查询单条数据
     *
     * @param majorId 主键
     * @return 实例对象
     */
    @Override
    public Major queryById(Integer majorId) {
        return this.majorDao.queryById(majorId);
    }

    /**
     * 分页查询
     *
     * @param major 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Major> queryByPage(Major major, PageRequest pageRequest) {
        long total = this.majorDao.count(major);
        return new PageImpl<>(this.majorDao.queryAllByLimit(major, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param major 实例对象
     * @return 实例对象
     */
    @Override
    public Major insert(Major major) {
        this.majorDao.insert(major);
        return major;
    }

    /**
     * 修改数据
     *
     * @param major 实例对象
     * @return 实例对象
     */
    @Override
    public Major update(Major major) {
        this.majorDao.update(major);
        return this.queryById(major.getMajorId());
    }

    /**
     * 通过主键删除数据
     *
     * @param majorId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer majorId) {
        return this.majorDao.deleteById(majorId) > 0;
    }

    @Override
    public List<Major> queryAll() {
        return this.majorDao.queryAll();
    }

    @Override
    public MajorDto getMajorDto() {
        List<Major> majorList = this.majorDao.queryAll();
        MajorDto majorDto = new MajorDto();
        majorDto.setMajorList(majorList);
        return majorDto;
    }
}
